package br.com.yahoo.mau_mss.tutorlucene.service.impl;

import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import br.com.yahoo.mau_mss.tutorlucene.service.CurriculumException;
import br.com.yahoo.mau_mss.tutorlucene.service.CurriculumService;

/**
 * Bean agendado para executar automaticamente a recriar do índice.
 * 
 * @author Paulo Sigrist / Wilson A. Higashino
 * @see CurriculumService
 */
@Service("scheduleBean")
public class ScheduleBean {
  private static final Logger log = Logger.getLogger(ScheduleBean.class.getName());
  @Inject
  private CurriculumService service;

  /**
   * Método que será invocado pelo schedule para reindexar os currículos a
   * cada minuto
   * @see CurriculumService#indexAll()
   * @see http://blog.voltdb.com/using-spring-schedule-annotation/
   */
  //@Schedule(minute = \"*/1\", hour = \"*\")
  @PostConstruct
  @Scheduled(cron="0 0 * * * *")
  public void reindex() {
    log.info("Recriando o índice");
    try {
      service.indexAll();
      log.info("Índice recriado com sucesso.");
    }
    catch (CurriculumException e) {
      log.throwing(ScheduleBean.class.getName(), "reindex", e);
    }

  }
}
